#include "kernel_operator.h"

constexpr int32_t TOTAL_LENGTH = 8 * 2048;                            // total length of data
constexpr int32_t USE_CORE_NUM = 8;                                   // num of core used
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;         // length computed of each core
constexpr int32_t TILE_NUM = 8;                                       // split data into 8 tiles for each core
constexpr int32_t BUFFER_NUM = 2;                                     // tensor num for each queue
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM; // separate to 2 parts, due to double buffer
constexpr uint32_t maskSize = (TILE_LENGTH + 7) / 8;

class KernelHardShrink {
public:
    __aicore__ inline KernelHardShrink() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, float lambda)  
    {
        this->lambda = lambda;  
        xGm.SetGlobalBuffer((__gm__ float *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        zGm.SetGlobalBuffer((__gm__ float *)z + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(tmpBuffer0, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(tmpBuffer1, maskSize * sizeof(uint8_t));
    }
    
    __aicore__ inline void Process()
    {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>();
        AscendC::DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
        inQueueX.EnQue(xLocal);
    }

    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.DeQue<float>();
        AscendC::LocalTensor<float> zLocal = outQueueZ.AllocTensor<float>();
        AscendC::LocalTensor<float> tmpLocal = tmpBuffer0.Get<float>();
        AscendC::LocalTensor<uint8_t> maskLocal = tmpBuffer1.Get<uint8_t>();
        float lambda = this->lambda;
        // 创建比较掩码：|x| > λ
        AscendC::Abs(tmpLocal, xLocal, TILE_LENGTH);          // |x|
        AscendC::CompareScalar(maskLocal, tmpLocal, lambda, AscendC::CMPMODE::GT, TILE_LENGTH);  // |x| > λ
        // 根据掩码选择：如果 |x| > λ 则选择 x，否则选择 0
        AscendC::Select(zLocal, maskLocal, xLocal, static_cast<float>(0),AscendC::SELMODE::VSEL_TENSOR_SCALAR_MODE ,TILE_LENGTH);
        outQueueZ.EnQue<float>(zLocal);
        inQueueX.FreeTensor(xLocal);
    }
    
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>();
        AscendC::DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tmpBuffer0;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tmpBuffer1;
    AscendC::GlobalTensor<float> xGm;
    AscendC::GlobalTensor<float> zGm;
    float lambda;
};

extern "C" __global__ __aicore__ void hard_shrink_custom(GM_ADDR x, GM_ADDR z, float lambda)
{
    KernelHardShrink op;
    op.Init(x, z, lambda);  
    op.Process();
}

#ifndef ASCENDC_CPU_DEBUG
void hard_shrink_custom_do(uint32_t blockDim, void *stream, uint8_t *x, uint8_t *z, float lambda)
{
    hard_shrink_custom<<<blockDim, nullptr, stream>>>(x, z, lambda);
}
#endif